package cn.hznc.controller.service.init;

import cn.hutool.core.collection.CollectionUtil;
import cn.hznc.constant.CommonConstant;
import cn.hznc.constant.InventoryStatusEnum;
import cn.hznc.constant.WarehouseConstant;
import cn.hznc.domain.entity.InventoryLocationEntity;
import cn.hznc.mapper.*;
import cn.hznc.repository.InventoryLocationRespository;
import cn.hznc.repository.ToolInventoryLocationRespository;
import cn.hznc.repository.ToolPartRespository;
import cn.hznc.repository.WarehouseRespository;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author：Administrator
 * @Date：2023/12/1 15:35
 */
@Service
public class InitService{

    @Resource
    private AllotEmployeeMapper allotEmployeeMapper;

    @Resource
    private AssemblyToolTypeMapper assemblyToolTypeMapper;

    @Resource
    private EntryExitRecordMapper entryExitRecordMapper;

    @Resource
    private EquipmentSubprogramMappingMapper equipmentSubprogramMappingMapper;

    @Resource
    private MachineToolTaskMapper machineToolTaskMapper;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private OrderCraftMapper orderCraftMapper;

    @Resource
    private OrderBomMapper orderBomMapper;

    @Resource
    private ProductionMapper productionMapper;

    @Resource
    private ProcessMapper processMapper;

    @Resource
    private ProductionBomMapper productionBomMapper;

    @Resource
    private ProductionProcessMapper productionProcessMapper;
    @Resource
    private ProgramMapper programMapper;

    @Resource
    private SubProgramMapper subProgramMapper;

    @Resource
    private ProcessCraftProgramMapper processCraftProgramMapper;

    @Resource
    private ToolForkInfoMapper toolForkInfoMapper;

    @Resource
    private ToolInventoryLocationMapper toolInventoryLocationMapper;

    @Resource
    private WarehouseRespository warehouseRespository;

    @Resource
    private InventoryLocationRespository inventoryLocationRespository;

    @Resource
    private ToolPartRespository toolPartRespository;

    @Resource
    private ProcessCraftMapper processCraftMapper;

    @Resource
    private ToolInventoryLocationRespository toolInventoryLocationRespository;


    public Object resetSystemData() {
        //清空人员排班表
        allotEmployeeMapper.delete(new QueryWrapper<>());
        //总成创建类型表
        assemblyToolTypeMapper.delete(new QueryWrapper<>());
        //清空出入库记录表
        entryExitRecordMapper.delete(new QueryWrapper<>());
        //清空任务表
        machineToolTaskMapper.delete(new QueryWrapper<>());
        //清空工单相关基础信息
        deleteOrderManageInfo();
        toolForkInfoMapper.delete(new QueryWrapper<>());
        //重置一级库和智能柜库存
        resetStock();
        return null;
    }

    @Transactional
    public void resetStock() {
        List<String> warehouseTypes = Arrays.asList(WarehouseConstant.FIRST_TOOL_CABINET, WarehouseConstant.INTELLIGENT_TOOL_CABINET);
        //List<String> warehouseTypes = Arrays.asList(WarehouseConstant.INTELLIGENT_TOOL_CABINET);
        //重置货架状态
        warehouseRespository.updateStatusByType(warehouseTypes, InventoryStatusEnum.EMPTY.getCode());
        List<Integer> warehouseIds = warehouseRespository.selectByType(warehouseTypes);
        //重置库位状态
        inventoryLocationRespository.updateStatusByWarehouseIds(warehouseIds ,  InventoryStatusEnum.EMPTY.getCode());
        //清空关联表数据
        toolInventoryLocationRespository.deleteByCondition(warehouseIds);
        //重置toolPart表中所有刀的库位
        toolPartRespository.deleteByLocation(warehouseTypes);
    }

    private void deleteOrderManageInfo() {
        //清空工单数据
        orderMapper.delete(new QueryWrapper<>());
        //情况工单快照数据
        orderCraftMapper.delete(new QueryWrapper<>());
        orderBomMapper.delete(new QueryWrapper<>());
        //清空产品相关数据
        productionMapper.delete(new QueryWrapper<>());
        productionBomMapper.delete(new QueryWrapper<>());
        processMapper.delete(new QueryWrapper<>());
        processCraftMapper.delete(new QueryWrapper<>());
        productionProcessMapper.delete(new QueryWrapper<>());
        processCraftProgramMapper.delete(new QueryWrapper<>());
        //清空机床程序对应表
        programMapper.delete(new QueryWrapper<>());
        equipmentSubprogramMappingMapper.delete(new QueryWrapper<>());
    }

    public void test() {
        //查询所有一级库的库位信息
        List<InventoryLocationEntity> updateList = new ArrayList<>();
        List<InventoryLocationEntity> inventoryLocationEntities = inventoryLocationRespository.selectAllListByWarehouseType("A001");
        if(CollectionUtil.isNotEmpty(inventoryLocationEntities)){
            for (InventoryLocationEntity inventoryLocationEntity : inventoryLocationEntities) {
                Integer countNumber = toolPartRespository.selectCountByLocationId(inventoryLocationEntity.getId());
                inventoryLocationEntity.setUseCapacity(countNumber);
                inventoryLocationRespository.update(inventoryLocationEntity);
                //updateList.add(inventoryLocationEntity);
            }
        }
//        if(CollectionUtil.isNotEmpty(updateList)){
//            inventoryLocationRespository.updateBatch(updateList);
//        }
    }

//    public void test1() {
//        toolPartRespository.deleteByCondition();
//    }
}
